package com.joysuch.wwyt.attendance.controller;


import com.joysuch.wwyt.attendance.bean.AttendanceGroupShiftListBean;
import com.joysuch.wwyt.attendance.bean.AttendanceShiftParam;
import com.joysuch.wwyt.attendance.dto.AttendanceShiftDto;
import com.joysuch.wwyt.attendance.entity.AttendanceShift;
import com.joysuch.wwyt.attendance.service.AttendanceShiftService;
import com.joysuch.wwyt.core.bean.SimpleListBean;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import com.joysuch.wwyt.core.exception.ApiBusinessException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 考勤班次
 * </p>
 *
 * @author zqw
 * @since 2022-02-28
 */
@RestController
@RequestMapping("/attendance/shift")
@Api(tags = "考勤班次")
public class AttendanceShiftController {

    @Autowired
    private AttendanceShiftService attendanceShiftService;

    @PostMapping("save")
    @ApiOperation("保存考勤班次")
    @RequiresPermissions("attendance:shift:save")
    public ResultBean save(@Valid @RequestBody AttendanceShiftDto dto) {
        AttendanceShift shift = null;
        try {
            shift = attendanceShiftService.saveShift(dto);
            return ResultBean.success(shift);
        } catch (ApiBusinessException e) {
            return ResultBean.wrapErrorResult(e.getCode(), e.getMessage());
        }
    }

    @GetMapping("findById")
    @ApiOperation("查看考勤班次")
    public ResultBean findById(@RequestParam(value = "id") Long id) {
        AttendanceShiftDto bean = attendanceShiftService.findById(id);
        return ResultBean.success(bean);
    }

    @PostMapping("pageShift")
    @ApiOperation("分页查看考勤班次")
    public ResultBean pageShift(@Valid @RequestBody AttendanceShiftParam pageParam) {
        return attendanceShiftService.pageShift(pageParam);
    }

    @GetMapping("deleteById")
    @ApiOperation("删除考勤班次")
    @RequiresPermissions("attendance:shift:deleteById")
    public ResultBean deleteById(@RequestParam(value = "id") Long id) {
        try {
            attendanceShiftService.deleteById(id);
            return ResultBean.defaultSuccessResult();
        } catch (ApiBusinessException e) {
            return ResultBean.wrapErrorResult(e.getCode(), e.getMessage());
        }
    }

    @GetMapping("getShiftsByGroupId")
    @ApiOperation("人员排班修改下拉列表")
    public ResultBean getShiftsByGroupId(@RequestParam(value = "groupId") Long groupId) {
        List<AttendanceGroupShiftListBean> list = attendanceShiftService.getShiftsByGroupId(groupId);
        return ResultBean.success(list);
    }

    @GetMapping("getTitleByGroupId")
    @ApiOperation("人员排班班次标题")
    public ResultBean getTitleByGroupId(@RequestParam(value = "groupId") Long groupId) {
        List<SimpleListBean> list = attendanceShiftService.getTitleByGroupId(groupId);
        return ResultBean.success(list);
    }
}

